package com.xiyu.service.service.elder;

import com.xiyu.service.vo.elder.accident.AccidentGetOutput;
import com.xiyu.service.vo.elder.accident.AccidentUpdateInput;
import com.xiyu.service.vo.elder.accident.AccidentCreateInput;
import com.xiyu.service.model.elder.register.ElderAccident;
import com.xiyu.service.repository.elder.register.ElderAccidentRepository;
import com.xiyu.service.vo.elder.accident.AccidentPageQueryOutput;
import com.xiyu.service.vo.elder.accident.AccidentPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.elder.AccidentConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.elder.AccidentErrorCode.*;

/**
 * 事故登记 Service 实现类
 */
@Service
@Validated
public class AccidentServiceImpl implements AccidentService {

    @Resource
    private ElderAccidentRepository elderAccidentRepository;

    @Override
    public PageResult<AccidentPageQueryOutput> pageQuery(AccidentPageQueryInput inputVO) {
        Page<ElderAccident> pageElderAccident = elderAccidentRepository.pageQuery(inputVO);
        List<AccidentPageQueryOutput> listElderAccident = AccidentConvert.INSTANCE.pageQueryOutputConvert(pageElderAccident.getRows());
        return new PageResult<>(listElderAccident, pageElderAccident.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(AccidentCreateInput inputVO) {
        ElderAccident newElderAccident = AccidentConvert.INSTANCE.createInputConvert(inputVO);
        newElderAccident = elderAccidentRepository.insert(newElderAccident);
        return newElderAccident.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(AccidentUpdateInput inputVO) {
        Optional<ElderAccident> optionalOldElderAccident = elderAccidentRepository.findById(inputVO.getId());
        if(!optionalOldElderAccident.isPresent()){
            throw exception(ELDER_ACCIDENT_NOT_EXIST);
        }
        ElderAccident updateElderAccident = AccidentConvert.INSTANCE.updateInputConvert(inputVO);
        elderAccidentRepository.update(updateElderAccident);
        return true;
    }

    @Override
    public AccidentGetOutput get(Long id) {
        Optional<ElderAccident> optionalElderAccident = elderAccidentRepository.findDetailById(id);
        if(!optionalElderAccident.isPresent()){
            throw exception(ELDER_ACCIDENT_NOT_EXIST);
        }
        return AccidentConvert.INSTANCE.getOutputConvert(optionalElderAccident.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleted(Long id) {
        Optional<ElderAccident> optionalElderAccident = elderAccidentRepository.findById(id);
        if(!optionalElderAccident.isPresent()){
            throw exception(ELDER_ACCIDENT_NOT_EXIST);
        }
        elderAccidentRepository.delete(optionalElderAccident.get());
        return true;
    }

}
